package com.gdut.sport.common;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtils {

    public static File convertBitmapToFile(Bitmap bitmap, String filename) {
        String path = Environment.getExternalStorageDirectory() + "/sport/snapshot/";
        Log.d("FileUtils", "dir path:" + path);
        File dir = new File(path);
        if (!dir.exists()) {
            Log.d("FileUtils", "make dir:" + path);
            dir.mkdirs();
        }
        File snapshot = new File(path + filename);
        Log.d("FileUtils", "snapshot path:" + snapshot.getAbsolutePath());
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(snapshot));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
            return snapshot;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
